﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WHC.OrderWater.Contract;
using WHC.OrderWater.Business;
using WHC.Hotel.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmEditDeliver : BaseForm
    {
        public string ID = string.Empty;

        public FrmEditDeliver()
        {
            InitializeComponent();
        }

        private void InitShopNames()
        {
            List<ShopInfo> shopList = BLLFactory<Shop>.Instance.GetAll();
            this.cmbShopName.Items.Clear();
            foreach (ShopInfo shopInfo in shopList)
            {
                this.cmbShopName.Items.Add(new CListItem(shopInfo.ShopName, shopInfo.ID));
            }

            int index = this.cmbShopName.FindStringExact(Portal.gc.CurrentShop.ShopName);
            this.cmbShopName.SelectedIndex = index;
        }

        private void SetInfo(DeliverManInfo info)
        {
            info.Name = txtName.Text;
            info.Address = txtAddress.Text;
            info.Note = txtNote.Text;

            CListItem item = cmbShopName.SelectedItem as CListItem;
            if (item != null)
            {
                info.Shop_ID = item.Value;
            }
            info.Sex = cmbSex.Text;
            info.BirthDay = dtpBirthDay.Value;
            info.Telephone1 = txtTelephone1.Text;
            info.Telephone2 = txtTelephone2.Text;
            info.Mobile = txtMobile.Text;
            info.Email = txtEmail.Text;
            info.LastUpdated = DateTime.Now;
            info.DisplayColor = this.txtDisplayColor.Text;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ID))
            {
                DeliverManInfo info = BLLFactory<DeliverMan>.Instance.FindByID(ID);
                if (info != null)
                {
                    SetInfo(info);

                    try
                    {
                        bool succeed = BLLFactory<DeliverMan>.Instance.Update(info, info.ID);
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
            else
            {
                DeliverManInfo info = new DeliverManInfo();
                SetInfo(info);
                info.ID = Guid.NewGuid().ToString();

                try
                {
                    bool succeed = BLLFactory<DeliverMan>.Instance.Insert(info);
                    if (succeed)
                    {
                        MessageUtil.ShowTips("保存成功");
                        this.DialogResult = DialogResult.OK;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
            }
        }

        private void FrmEditCustomer_Load(object sender, EventArgs e)
        {
            this.btnOK.Enabled = Portal.gc.HasFunction("BasicInfo/Deliver");

            InitShopNames();

            ToolTip tip = new ToolTip();
            tip.SetToolTip(this.txtDisplayColor, "用来区分用户发货记录的颜色");
            tip.SetToolTip(this.btnSelectColor, "用来区分用户发货记录的颜色");

            if (!string.IsNullOrEmpty(ID))
            {
                this.Text = "编辑 " + this.Text;
                DeliverManInfo info = BLLFactory<DeliverMan>.Instance.FindByID(ID);
                if (info != null)
                {
                    txtName.Text = info.Name;
                    txtAddress.Text = info.Address;
                    txtNote.Text = info.Note;
                    cmbSex.Text = info.Sex;

                    for (int i = 0; i < this.cmbShopName.Items.Count; i++)
                    {
                        CListItem item = this.cmbShopName.Items[i] as CListItem;
                        if (item.Value == info.Shop_ID)
                        {
                            this.cmbShopName.SelectedIndex = i;
                            break;
                        }
                    }                    

                    txtTelephone1.Text = info.Telephone1;
                    txtTelephone2.Text = info.Telephone2;
                    txtMobile.Text = info.Mobile;
                    txtEmail.Text = info.Email;
                    dtpBirthDay.Value = info.BirthDay;
                    lblCreateDate.Text = info.LastUpdated.ToString();

                    this.txtDisplayColor.Text = info.DisplayColor;
                    this.txtDisplayColor.BackColor = Portal.gc.GetColor(info.DisplayColor);
                }
            }
            else
            {
                this.Text = "新建 " + this.Text;
                lblCreateDate.Text = DateTime.Now.ToString();
            }
        }

        private void btnSelectColor_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txtDisplayColor.BackColor = dlg.Color;
                this.txtDisplayColor.Text = dlg.Color.ToArgb().ToString();
            }
        }
    }
}
